package app.Disthanitian.thaistyle.EmerMez;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends Activity {

	public static final String url = "http://192.168.2.56/emermez/login.php";
	public static String result = "";
	
	protected void onCreate(Bundle saveInstanceState) {
		super.onCreate(saveInstanceState);
		setContentView(R.layout.login);

		Button bt_login = (Button) findViewById(R.id.login);
		Button bt_signup = (Button) findViewById(R.id.signup1);
		Button bt_gomap = (Button) findViewById(R.id.goMap);
		Button bt_goStatus = (Button) findViewById(R.id.goStatus);
		TextView bt_forgetpass = (TextView) findViewById(R.id.forgetpassword);
		
		
		bt_goStatus.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(LoginActivity.this,ChangeStatus.class);
				startActivity(i);
					
			}
		});
		

		bt_gomap.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			Intent i = new Intent(LoginActivity.this,MainActivity.class);
			startActivity(i);
				
			}
		});
		bt_login.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				WebPageTask task = new WebPageTask();
				task.execute();

			}
		});
		
		
		bt_signup.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent newAct = new Intent(LoginActivity.this,RegisterActivity.class);
				startActivity(newAct);
			}
		});
		
		bt_forgetpass.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent newAct = new Intent(LoginActivity.this,ForgetPassword.class);
				startActivity(newAct);
				
			}
		});

	}

	public String login() {
		EditText edittext_email = (EditText) findViewById(R.id.edittext_email);
		EditText edittext_password = (EditText) findViewById(R.id.edittext_password);
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
		nameValuePairs.add(new BasicNameValuePair("email", edittext_email
				.getText().toString()));
		nameValuePairs.add(new BasicNameValuePair("password", edittext_password
				.getText().toString()));
		return postData(url, nameValuePairs);
	}

	public String postData(String url, List<NameValuePair> nameValuePairs) {
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(url);
		BufferedReader in = null;

		try {
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			HttpResponse response = httpclient.execute(httppost);
			in = new BufferedReader(new InputStreamReader(response.getEntity()
					.getContent()));
			StringBuffer sb = new StringBuffer("");
			String line = "";
			String NL = System.getProperty("line.separator");
			while ((line = in.readLine()) != null) {
				sb.append(line + NL);
			}
			in.close();
			String page = sb.toString();
			return page;
		} catch (ClientProtocolException e) {

		} catch (IOException e) {

		}
		return null;

	}

	protected class WebPageTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
			result = login();
			return result;
		}

		protected void onPostExecute(String result) {
			if (result.trim().equals("ok")) {
				Toast.makeText(LoginActivity.this, "Login success ",
						Toast.LENGTH_LONG).show();
			} else {
				Toast.makeText(LoginActivity.this,
						"Incorrect username and password ", Toast.LENGTH_LONG)
						.show();
                
                
			}
		}

	}

}
